package com.easylinkin.linkappapi.mechanical.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.mechanical.entity.EquipmentLocationData;
import com.easylinkin.linkappapi.mechanical.entity.MechanicalWorkRecord;
import com.easylinkin.linkappapi.mechanical.service.IEquipmentLocationDataService;
import com.easylinkin.linkappapi.mechanical.service.IMechanicalWorkRecordService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 机械工作记录表 前端控制器
 *
 * @author xy
 * @since 2025-06-17
 */
@Api(tags = "机械工作记录表")
@RestController
@RequestMapping("/mechanical/mechanicalWorkRecord")
public class MechanicalWorkRecordController {
    @Autowired
    private IMechanicalWorkRecordService mechanicalWorkRecordService;
    @Autowired
    private IEquipmentLocationDataService equipmentLocationDataService;
    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value="分页列表查询", notes="分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<MechanicalWorkRecord> requestModel) {
        // Assert.notNull(requestModel,"参数不能为空");
        // Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        MechanicalWorkRecord customQueryParams = requestModel.getCustomQueryParams();
        if (customQueryParams == null) {
            customQueryParams = new MechanicalWorkRecord();
            requestModel.setCustomQueryParams(customQueryParams);
        }
        customQueryParams.setTenantId(linkappUserContextProducer.getTenantId());
        IPage<MechanicalWorkRecord> pageList = mechanicalWorkRecordService.queryPageList(requestModel);
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Integer id) {
        MechanicalWorkRecord mechanicalWorkRecord = mechanicalWorkRecordService.getById(id);
        if(mechanicalWorkRecord == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        LambdaQueryWrapper<EquipmentLocationData> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EquipmentLocationData::getTenantId, mechanicalWorkRecord.getTenantId());
        wrapper.eq(EquipmentLocationData::getEquipmentId, mechanicalWorkRecord.getMechanicalId());
        wrapper.between(EquipmentLocationData::getLocationTime, mechanicalWorkRecord.getStartTime(), mechanicalWorkRecord.getEndTime());
        wrapper.eq(EquipmentLocationData::getMoveFlag, 1);
        wrapper.orderByDesc(EquipmentLocationData::getLocationTime);
        List<EquipmentLocationData> list = equipmentLocationDataService.list(wrapper);
        mechanicalWorkRecord.setLocationDataList(list);
        return RestBuilders.successBuilder().data(mechanicalWorkRecord).build();
    }
}
